﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using System.Collections.Generic;
using Matrix = Microsoft.Xna.Framework.Matrix;
using BlobTowerDefense.Managers;
using BlobTowerDefense.Tools;

namespace BlobTowerDefense.Game_Entities
{
    public class Wave
    {
        //seznam nepratel v dane vlne
        public WAVETYPE WaveType;
        private bool isLaunched;
        private List<ENEMYTYPE> enemyTypes;
        private int currentEnemyIndex = 0;

        public int WaveDifficultyModifier { get; private set; }

        private float[] intervalsSend;

        public Curve3D Path;

        public Wave(WAVETYPE waveType, int enemyAmount, ENEMYTYPE enemyType, float intervalSend, Curve3D path, int waveDifficultyModifier)
        {
            this.WaveType = waveType;
            this.intervalsSend = new float[enemyAmount];

            this.enemyTypes = new List<ENEMYTYPE>();
            for (int i = 0; i < enemyAmount; i++)
            {
                this.enemyTypes.Add(enemyType);
                this.intervalsSend[i] = intervalSend;
            }

            this.Path = path;

            this.WaveDifficultyModifier = waveDifficultyModifier;
        }

        public Wave(WAVETYPE waveType, int enemyAmount, ENEMYTYPE enemyType, float [] intervalsSend, int waveDifficultyModifier)
        {
            this.WaveType = waveType;

            enemyTypes = new List<ENEMYTYPE>();
            for (int i = 0; i < enemyAmount; i++)
            {
                enemyTypes.Add(enemyType);
            }

            this.intervalsSend = intervalsSend;

            this.WaveDifficultyModifier = waveDifficultyModifier;
        }

        public Wave(WAVETYPE waveType, List<ENEMYTYPE> enemyTypes, float intervalSend, int waveDifficultyModifier)
        {
            this.WaveType = waveType;
            this.intervalsSend = new float[enemyTypes.Count];

            this.enemyTypes = enemyTypes;

            for (int i = 0; i < enemyTypes.Count; i++)
            {
                this.intervalsSend[i] = intervalSend;
            }

            this.WaveDifficultyModifier = waveDifficultyModifier;
        }

        public Wave(WAVETYPE waveType, List<ENEMYTYPE> enemyTypes, float[] intervalsSend, int waveDifficultyModifier)
        {
            this.WaveType = waveType;

            this.enemyTypes = enemyTypes;

            this.intervalsSend = intervalsSend;

            this.WaveDifficultyModifier = waveDifficultyModifier;
        }

        public void Update()
        {
        }

        public void Launch()
        {
            isLaunched = true;
        }

        public void Pause()
        {
            isLaunched = false;
        }

        public bool WaveEnded()
        {
            return (currentEnemyIndex >= (enemyTypes.Count - 1));
        }

        public bool WaveRunning()
        {
            return isLaunched;
        }

        public float? TimeToLaunchNextEnemy()
        {
            float? timeToLaunch = 0.0f;
            for (int i = 0; i <= currentEnemyIndex; i++)
            {
                if (currentEnemyIndex < intervalsSend.Length)
                {
                    timeToLaunch += intervalsSend[i];
                }
                else
                {
                    timeToLaunch = null;
                }
            }
            return timeToLaunch;
        }

        public ENEMYTYPE? NextEnemy()
        {
            ENEMYTYPE? enemyTypeToReturn;

            if (isLaunched)
            {
                if (currentEnemyIndex < (enemyTypes.Count - 1))
                {
                    enemyTypeToReturn = enemyTypes[currentEnemyIndex];
                    currentEnemyIndex++;
                }
                else
                {
                    enemyTypeToReturn = null;
                }
            }
            else
            {
                enemyTypeToReturn = null;
            }
            return enemyTypeToReturn;
        }

        public List<ENEMYTYPE> GetTypesOfEnemies()
        {
            List<ENEMYTYPE> enemyTypesToReturn = new List<ENEMYTYPE>();
            foreach(ENEMYTYPE enemyType in this.enemyTypes)
            {
                if (!enemyTypesToReturn.Contains(enemyType))
                {
                    enemyTypesToReturn.Add(enemyType);
                }
            }

            return enemyTypesToReturn;
        }
    }
}
